package com.zlyx.easy.web.generate;

import java.util.Map;

import org.springframework.context.ApplicationContext;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zlyx.easy.core.annotations.ApplicationBean;
import com.zlyx.easy.core.event.IHandlerOnRefreshed;
import com.zlyx.easy.core.utils.SpringUtils;
import com.zlyx.easy.database.annotations.TableBean;
import com.zlyx.easy.web.generate.annotations.EasyGenerater;
import com.zlyx.easy.web.generate.core.AbstractGenerator;

/**
 * @Auth 赵光
 * @Describle
 * @2019年1月5日 下午1:05:15
 */
@ApplicationBean(todo = { "代码生成器管理中心" })
public class GeneratorManager implements IHandlerOnRefreshed {

	@Override
	public void doOnRefreshed(ApplicationContext context) {
		Map<String, Object> tableBeans = SpringUtils.getBeanswithAnnotation(TableBean.class);
		Map<String, ?> generators = SpringUtils.getBeansOfType(AbstractGenerator.class);
		if (generators != null && generators.size() > 0) {
			AbstractGenerator generator = null;
			EasyGenerater easyGenerater = null;
			for (String generaterName : generators.keySet()) {
				generator = (AbstractGenerator) generators.get(generaterName);
				if (!ObjectUtils.isEmpty(generator)) {
					easyGenerater = generator.getClass().getAnnotation(EasyGenerater.class);
					if (!(easyGenerater != null && !easyGenerater.isGenerate()) && generator.isGenerate()) {
						generator.run(tableBeans);
					}
				}
			}
		}
	}

}
